﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Xml;

namespace Noea.Parser
{
    // HtmlDocDom uses XMLReader in .NET, which is based on recursive descend. Note: This only works on valid HTML documents.
    public class HTMLDocDom : IHTMLDoc
    {
        private string doc;

        public HTMLDocDom(string doc)
        {
			this.doc = doc;
        }

        // read-only property. Returns the web page as a string.
        public string Document
        {
            get { return doc; }
        }

        // Returns a list of strings containing the urls of all links to pages on the this site.
        // That is: links to web pages outside this are not included
        public IList GetAllLinks()
        {
			IList links = new List<string>();
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.LoadXml(doc);
			XmlNode root = xmlDoc.DocumentElement;
			foreach (XmlNode item in root.SelectNodes("//a"))
				links.Add(item.Attributes["href"].Value);

			return links;
        }

        // returns true if searchString is found in <body>...</body> in this HTML-document
        public bool Contains(string searchString)
        {
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.LoadXml(doc);
			XmlNode root = xmlDoc.DocumentElement;

			if (root.InnerXml.Contains(searchString))
				return true;

			return false;
        }


    }
}